package com.itbaizhan.hot.service.impl;

import com.itbaizhan.common.redis.dao.RedisDao;
import com.itbaizhan.common.vo.LivegoodsResult;
import com.itbaizhan.hot.mapper.HotMapper;
import com.itbaizhan.hot.service.HotService;
import com.itbaizhan.pojo.Product;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 业务层
 */
@DubboService
public class HotServiceImpl implements HotService {
    @Autowired
    private HotMapper hotMapper;
    @Value("${livegoods.nginx.host}")
    private String nginxHost;
    @Value("${livegoods.redis.hot}")
    private String hotKey;
    @Autowired
    private RedisDao redisDao;

    @Override
    public LivegoodsResult showHotProduct(String city) {
        if(redisDao.hasKey(hotKey+":"+city)){
            System.out.println("热销-缓存");
            return redisDao.get(hotKey+":"+city);
        }
        System.out.println("热销-mysql");
        List<Product> products = hotMapper.selectSales(city);
        products.forEach(product -> {
            product.setImg(nginxHost+product.getImg());
        });
        LivegoodsResult result = LivegoodsResult.ok(products);
        redisDao.set(hotKey+":"+city,result,7, TimeUnit.DAYS);
        return result;
    }

    @Value("${livegoods.redis.recommend}")
    private String recommendKey;

    @Override
    public LivegoodsResult showRecommendProduct(String city) {
        if(redisDao.hasKey(recommendKey+":"+city)){
            return redisDao.get(recommendKey+":"+city);
        }
        List<Product> products = hotMapper.selectRecommond(city);
        products.forEach(product -> {
            product.setImg(nginxHost+product.getImg());
        });
        LivegoodsResult result = LivegoodsResult.ok(products);
        redisDao.set(recommendKey+":"+city,result,7, TimeUnit.DAYS);
        return result;
    }
}
